/**
  ******************************************************************************
  * @file    log-handle.h
  * @author  xiaohu
  * @version V1.0.0
  * @date    02-22-2016
  * @brief   加固日志处理
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2016 上海今中网络科技有限公司 </center></h2>
  ******************************************************************************
  */
#include "log-handle.h"

LOG_PRI_T         logRpi;
LOG_HEAD_T        logHead;
LOG_MSG_T         logMsg;
LOG_TYPE_INFO_T   logTypeInfo;

long userLoginOffTime=0L;
char userLoginTime[30]={0};
char priAll[4]={0};

/**
  *@brief 从socket buf中获取日志的信息
  *@param
  *@return
  */
void get_loginfo_form_buff(char * buff)
{
    char buffCp[1024]={0};
    char *savep = NULL;

    strcpy(buffCp,buff);
    char *firstString;
    firstString = strtok_r(buffCp, ":", &savep);
    strcpy(priAll,firstString);
    logRpi.facility=atoi(firstString) >> 3;
    logRpi.severity=atoi(firstString) - logRpi.facility*8;

    char *secondString;
    secondString = strtok_r(NULL, ":", &savep);
    logMsg.violationType=atoi(secondString);

    char *thirdString;
    thirdString = strtok_r(NULL, ":", &savep);
    logMsg.handle=atoi(thirdString);

    char *fourthString;
    fourthString = strtok_r(NULL, ":", &savep);

    if (strcmp(fourthString,DEFAULT_DETAIL)==0)
    {
        if(logMsg.violationType==PC_TWOWAY)
            logMsg.detail=strdup(PC_TWOWAY_DETAIL);
        if(logMsg.violationType==PC_PROCESSRUNNING)
            logMsg.detail=strdup(PC_PROCESSRUNNING_DETAIL);
        if(logMsg.violationType==AD_TWOWAY)
            logMsg.detail=strdup(AD_TWOWAY_DETAIL);
        if(logMsg.violationType==AD_SHARE)
            logMsg.detail=strdup(AD_SHARE_DETAIL);
        if(logMsg.violationType==AD_BLUETOOTH)
            logMsg.detail=strdup(AD_BLUETOOTH_DETAIL);
    }
    else
    {
        logMsg.detail=strdup(fourthString);
    }

}

/**
  *@brief 从本地中获取日志的信息
  *@param
  *@return
  */
int get_loginfo_form_local(char * userName)
{
    time_t        now;
    char          sqlBuffer[SQL_BUFLEN]    = {0};
    int           rows                     = 0;
    int           cols                     = 0;
    char         *errmsg                   = NULL;
    char        **result                   = NULL;

    logHead.violationTime = time(&now);
    logHead.userName = strdup(userName);
    //if( logMsg.handle == REMOVE_CLIENT || logRpi.severity<=Error){
    if( logMsg.handle == REMOVE_CLIENT )
    {
        userLoginOffTime = logHead.violationTime;
        MSG_DEBUG("下线");
    }
    /// 查询用户信息表
    snprintf(sqlBuffer, SQL_BUFLEN, "SELECT userLoginTime FROM MPSInfo "
                                    "WHERE userName = \'%s\'", userName);
    if (QueryDataFromDatabase(dbHandle, sqlBuffer, result, rows, cols, errmsg) != SQLITE_OK)
    {
        MSG(LOG_ERROR, "[getLogInfoFormLocal] %s!", errmsg);
        sqlite3_free(errmsg);
        return RETURN_FAILURE;
    }
    if (rows == 0)
    {
        return RETURN_FAILURE;
    }
    else
    {
        strcpy(userLoginTime,result[1]);
        FreeQueryDataFromDatabase(result);
        return RETURN_SUCCESS;
    }

}


/**
  *@brief 写日志导数据库
  *@param
  *@return
  */
int get_log_to_sql()
{

    /* */
    char          sqlBuffer[SQL_BUFLEN]    = {0};
    char         *errmsg                   = NULL;
    /******patched xiaohu add 2016/02/22****************************/
    /* 进行日志数目的检查*/
    const char * tableName = "tbZdLog";
    long columns = get_table_columns(ZDLogDbHandle, tableName);
    if(columns == -1)
        return RETURN_FAILURE;
    else if(columns > ServerConfig.maxNumDBLog)
        del_table_columns(ZDLogDbHandle, tableName, 0, ServerConfig.delNumMaxDBLog);
    /**************************************************************/
    if (userLoginOffTime != 0L )
    {
        snprintf(sqlBuffer, SQL_BUFLEN,
                "INSERT INTO tbZdLog (pri,violationTime,userLoginOffTime, \
                userLoginTime,userName,violationType,handle,detail) \
                VALUES( %d , %ld , %ld , \'%s\' , \'%s\' , %d , %d , \'%s\' );",
                atoi(priAll),logHead.violationTime,userLoginOffTime,
                userLoginTime,logHead.userName,logMsg.violationType,logMsg.handle,logMsg.detail);
        userLoginOffTime = 0L;
    }
    else
    {
        snprintf(sqlBuffer, SQL_BUFLEN,
                "INSERT INTO tbZdLog (pri,violationTime, \
                userLoginTime,userName,violationType,handle,detail) \
                VALUES( %d , %ld , \'%s\' , \'%s\' , %d , %d , \'%s\' );",
                atoi(priAll),logHead.violationTime,
                userLoginTime,logHead.userName,logMsg.violationType,logMsg.handle,logMsg.detail);
    }
    MSG_DEBUG(sqlBuffer);
    if (InsertDataIntoDatabase(ZDLogDbHandle, sqlBuffer, errmsg) != SQLITE_OK)
    {
        MSG(LOG_ERROR, "[getLogToSql] %s!", errmsg);
        sqlite3_free(errmsg);
        //goto finish;
        return RETURN_FAILURE;
    }
    return RETURN_SUCCESS;
}

